package com.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.pay.config.AlipayTemplate;
import com.pay.fegin.HouseFeignService;
import com.pay.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lins
 * @date 2021/10/21 14:52
 */
@Service
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    private AlipayTemplate alipayTemplate;
    @Autowired
    private HouseFeignService houseFeignService;
    @Override
    public AlipayTradeRefundResponse rebund(Integer examineId) {

//        String data = houseFeignService.getOrderSnByExamineId(examineId);
//        HongyeOrderVo orderVo = JSON.parseObject(data, HongyeOrderVo.class);
//        AlipayTradeRefundResponse response = null;
//        if (orderVo!=null){
//            AlipayClient alipayClient = new DefaultAlipayClient(
//                    alipayTemplate.getGatewayUrl(),
//                    alipayTemplate.getApp_id(),
//                    alipayTemplate.getMerchant_private_key(),
//                    "json",
//                    "GBK",
//                    alipayTemplate.getAlipay_public_key(),
//                    "RSA2");
//            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
//            JSONObject bizContent = new JSONObject();
//            bizContent.put("out_trade_no", orderVo.getOrderNumber());
//            bizContent.put("refund_amount", orderVo.getOrderPrice());
//            bizContent.put("out_request_no", UUID.randomUUID().toString());
//
//            request.setBizContent(bizContent.toString());
//
//            try {
//                response = alipayClient.execute(request);
//            } catch (AlipayApiException e) {
//                e.printStackTrace();
//            }
//        }

        return null;
    }

    @Override
    public AlipayTradeQueryResponse transactionQuery(String tradeNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                alipayTemplate.getGatewayUrl(),
                alipayTemplate.getApp_id(),
                alipayTemplate.getMerchant_private_key(),
                "json",
                "GBK",
                alipayTemplate.getAlipay_public_key(),
                "RSA2"
        );
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
//        bizContent.put("out_trade_no", "20150320010101001");
        bizContent.put("out_trade_no", tradeNo);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response;
    }
}
